﻿using System;
using System.Collections.Generic;
using System.Web;
using ZLDC.Common.Estate;
using System.Data;
using ZLDC.Topo.Model.Estate;

namespace ZLDC.Estate
{
    public class CHandlerSystem
    {
                                        public static void ChooseMethod()
        {
            
            string act = _Request.GetString("act", "");

            switch (act)
            {
                #region 系统常量

                case "systemparam_list":
                    SystemParamList();
                    break;
                case "systemparam_list_bytype":
                    SystemParamListByType();
                    break;
                case "systemparam_del":
                    SystemParamDel();
                    break;
                case "systemparam_save":
                    SystemParamSave();
                    break;
                case "systemparam_detail":
                    SystemParamDetail();
                    break;



                #endregion

                #region 系统公告
                case "p_news_list":
                    p_news_list();
                    break;
                case "p_news_list_table":
                    p_news_list_table();
                    break;
                #endregion

                #region 修改密码
                case "UpDatePwd":
                    UpdateZLDCHRPwd();
                    break;
                #endregion
                #region 用户配置列

                case "useroption_hide":
                    UseroptionHide();
                    break;
                case "useroption_detail":
                    UseroptionDetail();
                    break;
                case "useroption_save":
                    UseroptionSave();
                    break;

                #endregion

                #region 系统操作日志

                case "operationlog_list":
                    OperationlogList();
                    break;

                #endregion

                #region 系统设置

                case "systemoption_list":
                    SystemoptionList();
                    break;
                case "systemoption_save":
                    SystemoptionSave();
                    break;

                #endregion

                #region 注销
                case "CancellationUserInfo":
                    CancellationUserInfo();
                    break;
                #endregion
            }
        }

        #region 系统常量

                                private static void SystemParamList()
        {
            int page = _Request.GetInt("page", 0);
            int pagesize = _Request.GetInt("pagesize", 0);
            string type = _Request.GetString("type", "");

            string condition = "";
            if (type != "")
                condition = " where isValid = 0 and type='" + type + "'";

            string json = ZLDC.Topo.BLL.Estate.SystemParamBLL.SystemParamList(page, pagesize, condition);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

                                                public static void SystemParamListByType()
        {
            string type = _Request.GetString("type", "");
            string json = ZLDC.Topo.BLL.Estate.SystemParamBLL.SelectItemByType(type);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

                                private static void SystemParamDel()
        {
            int id = _Request.GetInt("id", 0);

            string outmsg = ZLDC.Topo.BLL.Estate.SystemParamBLL.SystemParamDelete(id);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(outmsg);
            HttpContext.Current.Response.End();
        }

                                private static void SystemParamSave()
        {
            int id = _Request.GetIntForm("hID", 0);

            string type = _Request.GetStringForm("sType", "");               if (type == "手填" || type == "")
                type = _Request.GetStringForm("iType", "");

            string item = _Request.GetStringForm("iItem", "");
            string mark3 = _Request.GetStringForm("taRemark", "");
            string mark2 = _Request.GetString("mark2", "");
            string mark1 = _Request.GetString("mark1", "");

            string outmsg = ZLDC.Topo.BLL.Estate.SystemParamBLL.SystemParamSave(id, type, item, mark3, mark2, mark1);

            _Utility.GoError(outmsg);
        }

                                private static void SystemParamDetail()
        {
            int id = _Request.GetInt("id", 0);

            string json = ZLDC.Topo.BLL.Estate.SystemParamBLL.SystemParamDetail(id);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

        #endregion

        #region 用户配置列

                                private static void UseroptionHide()
        {
            int userid = _Convert.ToInt(_Utility.GetInfoBySession().m_sUserID, 0);
            int typeid = _Request.GetInt("typeid", 0);
            string itemvalue = "";

            string type = "";
            if (typeid == 1)
                type = "盘源列";
            else if (typeid == 2)
                type = "客源列";
            else if (typeid == 3)
                type = "楼盘字典列";

            using (_Database db = new _Database("estate"))
            {
                UserOptionInfo info = ZLDC.DAL.Estate.UserOptionDAL.p_user_option_detail(typeid, userid, db);

                if (info != null)
                    itemvalue = info.FieldName;
            }


            string json = ZLDC.Topo.BLL.Estate.SystemParamBLL.SelectItemByType(type, itemvalue);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

                                private static void UseroptionDetail()
        {
            int typeid = _Request.GetInt("typeid", 0);              int userid = _Convert.ToInt(_Utility.GetInfoBySession().m_sUserID, 0);

            string json = ZLDC.Topo.BLL.Estate.UserOptionBLL.p_user_option_detail(typeid, userid);
            json = "{" + json + "}";

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

                                private static void UseroptionSave()
        {
            int typeid = _Request.GetInt("typeid", 0);              int userid = _Convert.ToInt(_Utility.GetInfoBySession().m_sUserID, 0);
            string modifyUser = _Utility.GetInfoBySession().m_sUserName;
            string fieldName = _Request.GetString("fieldname", "");                 string fields = _Request.GetString("fields", "");                       string mark1 = _Request.GetString("mark1", "");             
            string json = ZLDC.Topo.BLL.Estate.UserOptionBLL.p_user_option_save(userid, typeid, fieldName, fields, modifyUser, 0, mark1, mark1, mark1);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }


        #endregion

        #region 系统操作日志

                                private static void OperationlogList()
        {
            int page = _Request.GetInt("page", 0);
            int pagesize = _Request.GetInt("pagesize", 0);
            string condition = " where 1=1 ";
            string station = _Utility.GetInfoBySession().m_sStation;                            string allchildrenDept = _Utility.GetInfoBySession().m_sChildrenDeptList;
            string SalesManagerStation = _Utility.GetWebConfigSetting("SalesManagerStation");

            if (_Utility.GetInfoBySession().m_sDeptID != "22")            {
                if (SalesManagerStation.Contains(station))
                    condition += " and operatedeptid in(" + allchildrenDept + ")";
                else
                    condition += " and operateuserid = " + _Utility.GetInfoBySession().m_sUserID;
            }

            int typeid = _Request.GetInt("typeid", 0);               string operateType = "";
            switch (typeid)
            {
                case 1:
                    operateType = "系统登录";
                    break;
                case 2:
                    operateType = "楼盘字典";
                    break;
                case 3:
                    operateType = "盘源管理";
                    break;
                case 4:
                    operateType = "客源管理";
                    break;

            }

            if (typeid > 0)
                condition += " and operateType = '" + operateType + "'";
            else
            {
                condition += " and operateType = '系统登录' ";
            }
            string startdate = _Request.GetString("startdate", "");
            string enddate = _Request.GetString("enddate", "");
            string name = _Request.GetString("name", "");
            int deptid = _Request.GetInt("deptid", 0);
            if (startdate == "" && enddate == "")            {
                condition += " and DATEDIFF(WEEK,operateDate,GETDATE())<=2 ";
            }
            if (startdate != "" && enddate != "")
                condition += " and operateDate > '" + startdate + "' and operateDate < '" + enddate + "'";

            if (deptid > 0)
            {
                condition += " and operatedeptid=" + deptid + " ";
            }


            if (name != "")
            {
                string userName = string.Empty;                 string userNo = string.Empty;                   int userId = 0;
                _Utility.GetUserNoAndUserName(name.Trim(), out userName, out userNo);
                userId = ZLDC.Topo.BLL.Estate.UserBLL.GetUserIDByUserNo(userNo);
                condition += " and operateUserId=" + userId + "";

            }
            string json = ZLDC.Topo.BLL.Estate.OperationlogBLL.p_operationlog_list_table(page, pagesize, condition);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }


        #endregion

        #region 系统设置

                                private static void SystemoptionList()
        {
            int typeid = _Request.GetInt("typeid", 0);  
            string json = ZLDC.Topo.BLL.Estate.SystemOptionBLL.p_system_option_listall_table(typeid);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

                                private static void SystemoptionSave()
        {
            DataTable dt = null;
            int typeid = _Request.GetIntForm("hTypeID", 0);              int subsidiaryID = 0;
            string subsidiary = "总公司";
            string modifyUser = _Utility.GetInfoBySession().m_sUserName;

            using (_Database db = new _Database("estate"))
            {                
                dt = ZLDC.DAL.Estate.SystemOptionDAL.p_system_option_listall_table(typeid, db);
            }

            string outmsg = "";
            int id = 0;
            int itemvalue = 0;                  string remark = "";                 if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    id = _Convert.ToInt(dt.Rows[i]["id"].ToString(), 0);

                    itemvalue = _Request.GetIntForm("itemvalue_" + id, 0);
                    remark = _Request.GetStringForm("iRemark_" + id, "");

                                        ZLDC.Topo.BLL.Estate.OperationlogBLL.p_operationlog_insert("t_system_option", id, "系统设置", "系统参数被修改为:" + itemvalue);

                    outmsg = ZLDC.Topo.BLL.Estate.SystemOptionBLL.p_system_option_save(id, subsidiaryID, subsidiary, typeid, "", itemvalue, "", "", 0, modifyUser, remark,0);
                }
            }

            _Utility.GoError(outmsg);
        }

        #endregion

                                public static void UpdateZLDCHRPwd()
        {
            string pwd = _Request.GetString("pwd", "");

            int userid = 0;            userid = _Request.GetInt("userid", 0);
            if (userid == 0)
            {
                userid = _Convert.ToInt(_Utility.GetInfoBySession().m_sEHRUserID);            }
            string status = _Request.GetString("status", "");
            if (status == "")
            {
                status = _Utility.GetInfoBySession().m_sStatus;            }
            string json = ZLDC.Topo.BLL.Estate.UserBLL.UpdateZlhqPwd(userid, pwd, status);
            if (json != "")
            {
                                ZLDC.Topo.BLL.Estate.OperationlogBLL.p_operationlog_insert("t_user", userid, 5, "还原密码");
            }

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }
                                public static void p_news_list()
        {
            string condition = " where dltflg=0 ";
            int typeid = _Request.GetInt("typeid", 0);
            if (typeid == 0)            {
                condition += " and DATEDIFF(MM,pubdate,GETDATE())=0 ";
            }
            string json = ZLDC.Topo.BLL.Estate.NewsBLL.p_News_List(condition);
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

                                public static void p_news_list_table() 
        {
            int page = _Request.GetInt("page", 0);
            int pagesize = _Request.GetInt("pagesize", 0);

            string json = ZLDC.Topo.BLL.Estate.NewsBLL.p_news_list_table(page, pagesize);

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }

                                public static void CancellationUserInfo() 
        {
            HttpContext.Current.Session.Abandon();
            HttpContext.Current.Session.Clear();

            string json = "true";

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(json);
            HttpContext.Current.Response.End();
        }
    }
}
